(function(){
    var heught_l=$(".lian_seach").height();
    $(".du_seach").css("height",heught_l);
    $.ajax({
        type: 'POST',
        url: 'fourCascades',
        dataType: 'JSON',
        data: {},
        success: function(data){
            var province  = data.province;
            for (var i  in province) {
                var str = "<option value='"+i+"'>"+province[i]+"</option>";
                $('.province').append(str);
            }
            var city = data.city;
            $('.province').change(function(){
                var index = this.selectedIndex;
                window.localStorage.setItem('provinceid', index);
                var children = $('.city').children();
                for (var i = 0; i < children.length; i++) {
                    children[i].remove();
                }
                var children = $('.gw').children();
                for (var i = 0; i < children.length; i++) {
                    children[i].remove();
                }
                var children = $('.way').children();
                for (var i = 0; i < children.length; i++) {
                    children[i].remove();
                }
                var str = "<option value='0'>请选择城市</option>";
                $('.city').append(str);
                var str1 = "<option value='0'>请选择GW</option>";
                $('.gw').append(str1);
                var str2 = "<option value='0'>请选择渠道</option>";
                $('.way').append(str2);
                for (var i in city[index - 1]) {
                    var str = "<option value='"+i+"'>"+city[index - 1][i]+"</option>";
                    $('.city').append(str);
                }

            });
            var gwusernum = data.gws;
            $('.city').change(function(){
                var cityIndex = this.selectedIndex;
                window.localStorage.setItem('cityid', cityIndex);
                var children = $('.gw').children();
                for (var i = 0; i < children.length; i++) {
                    children[i].remove();
                }
                var children = $('.way').children();
                for (var i = 0; i < children.length; i++) {
                    children[i].remove();
                }
                var str = "<option value='' class='gwusernum'>请选择GW</option>";
                $('.gw').append(str);
                var str1 = "<option value='' class='gwusernum'>请选择渠道</option>";
                $('.way').append(str1);
                var provinceid = window.localStorage.getItem('provinceid');
                var gws = gwusernum[provinceid - 1];
                for (var i in gws[cityIndex - 1]) {
                    var str = "<option value='"+gws[cityIndex - 1][i]+"' class='gwusernum'>"+gws[cityIndex - 1][i]+"</option>";
                    $('.gw').append(str);
                }
            });

            var waynums = data.waynums;
            $('.gw').change(function(){
                var index = this.selectedIndex;
                var children = $('.way').children();
                for (var i = 0; i < children.length; i++) {
                    children[i].remove();
                }
                var str = "<option value='' class='wayusernum'>请选择渠道</option>";
                $('.way').append(str);
                var provinceid = window.localStorage.getItem('provinceid');
                var cityid = window.localStorage.getItem('cityid');
                var way = waynums[provinceid - 1][cityid - 1];
                for (var i in way[index - 1]) {
                    var str = "<option value='"+way[index - 1][i]+"' class='wayusernum'>"+way[index - 1][i]+"</option>";
                    $('.way').append(str);
                }
            });
        }
    });
}());


window.onresize = function() {
    var heught_l = $(".lian_seach").height();
    $(".du_seach").css("height", heught_l);
};

$('.btn_seach').click(function () {
    var id = $(".gwusernum").val();
    if (id == '') {
        sweetAlert('', '订单号或岗位号不能为空', 'error');
        return;
    }
    $.ajax({
        type: 'POST',
        url: 'findOrderInfo',
        dataType: 'JSON',
        data: {id: id},
        success: function (res) {
            if (res.error == 1 || res.msg.length <= 0) {
                addNotify();
                return;
            }
            var data = res.msg;
            setItem('orders', data);
            var totalPage = Math.ceil(data.length / 15);
            showPage(1, totalPage, addInfo);recovery();
            addInfo(1, totalPage);
        },
        beforeSend: function() {
            $('.modal_Load').show();
        },
        complete: function() {
            $('.modal_Load').hide();
        },
        error: function (error) {
            console.log(error);
        }
    });
});

$('.lian_btn_seach').click(function () {
    selectOrderInfo(1);
});

function selectOrderInfo(page)
{
    var times = [];
    var stringTime = $('.text-box-start').val();
    var timestamp = Date.parse(new Date(stringTime)) / 1000;
    var stringTime1 = $('.text-box-end').val();
    var timestamp2 = Date.parse(new Date(stringTime1)) / 1000;
    if (timestamp && timestamp2) {
        if (timestamp > timestamp2) {
            sweetAlert('','结束时间不能小于起始时间', 'error');
            return;
        }
        times = [timestamp, timestamp2];
    }
    var provinceId = $('.province').val();
    var cityId = $('.city').val();
    var gwusernum = $('.gw').val();
    var waynum = $('.way').val();
    var orderStatus = $('.order').val();
    var gwStatus = $('.gwstatus').val();
    var wayStatus = $('.wayuser').val();

    var data = {
        province: provinceId,
        city: cityId,
        gwusernum: gwusernum,
        waynum: waynum,
        orderStatus: orderStatus,
        gwStatus: gwStatus,
        wayStatus: wayStatus,
        times: times
    };
    $.ajax({
        type: 'POST',
        url: '/admin/GW/selectOrderInfo',
        dataType: 'JSON',
        data: data,
        success: function (res) {
            if (res.msg.length <= 0 || res.error == 1) {
                addNotify();
                return;
            }
            var data = res.msg;
            setItem('orders', data);
            var totalPage = Math.ceil(data.length / 15);
            showPage(1, totalPage, addInfo);recovery();
            addInfo(page, totalPage);

        },
        beforeSend: function() {
            $('.modal_Load').show();
        },
        complete: function() {
            $('.modal_Load').hide();
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function addInfo(page, totalPage) {
    var data = pageData(page);
    showPage(page, totalPage, addInfo);
    if (data.length <= 0) {
        addNotify();
        return;
    }
    $('.table_hover tr').remove();
    for (var i = 0; i < data.length; i ++) {
        var status = '';
        switch (data[i].status) {
            case "0":
                status = '待风控人员审核';
                break;
            case "1":
                status = "待城市经理审核";
                break;
            case "2":
                status = "待用户签约";
                break;
            case "3":
                status = "待确认收款";
                break;
            case "4":
                status = "待车辆上牌";
                break;
            case "5":
                status = "车辆已上牌";
                break;
            case "6":
                status = "已交付订单";
                break;
            case "-2":
                status = "暂拒订单";
                break;
            case "-1":
                status = "被拒订单";
                break;
            case "7":
                status = '待风控经理审核'
        }
        var waystatus = '';
        switch(data[i].waystatus) {
            case "2":
                waystatus = "已激活";
                break;
            case "3":
                waystatus = "已关闭";
                break;
        }
        var str = "";
        str += "<td>"+(i+1)+"</td>";
        str += "<td>"+data[i].orderid+"</td>";
        str += "<td>"+data[i].create_time+"</td>";
        str += "<td>"+status+"</td>";
        str += "<td>"+data[i].overdue+"</td>";
        str += "<td>"+data[i].username+"</td>";
        str += "<td>"+data[i].tel+"</td>";
        str += "<td>"+data[i].waynum+"</td>";
        str += "<td>"+waystatus+"</td>";
        str += "<td>"+data[i].gwname+"</td>";
        str += "<td>"+data[i].gwusernum+"</td>";
        str += "<td>"+data[i].city+"</td>";
        str += "<td>"+data[i].csusernum+"</td>";
        str += "<td>"+data[i].province+"</td>";
        str += "<td>"+data[i].szusernum+"</td>";
        str += "<td>"+data[i].jdc_price+"</td>";
        str += "<td>"+data[i].money+"</td>";
        str += "<td>"+data[i].returnend+"</td>";
        str += "<td>"+data[i].notend+"</td>";

        var times = data[i].end_times;
        for (var j = 0; j < times.length; j ++) {
            if (times[j].overdue_status == 1) {
                str += "<td style='color:red'>"+times[j].pay_time+"</td>";
            } else if (times[j].pay_time) {
                str += "<td>"+times[j].pay_time+"</td>";
            } else {
                str += "<td>-</td>";
            }
        }
        if (times.length <= 0) {
            for  (var j = 0; j < 12; j ++) {
                str += "<td>暂无数据</td>";
            }
        }
        var thstr = "<tr>"+str+"</tr>";
        $('.table_hover').append(thstr);
    }
    $('.tr_checkbox').show();
    if (!filterTag) {
        tableHide();
    } else {
        recovery();
    }
}

$('.button_export').click(function(){
    var status = $(this).val();
    $.ajax({
        type: 'POST',
        url: 'exportToExcelOrderList',
        dataType: 'JSON',
        data: {status: status, filterIndexs: filterIndexs, filterNames: filterNames, filterTag: filterTag},
        success: function(response){
            location.href = response.path;
        },
        beforeSend: function() {
            $('.modal_Load').show();
        },
        complete: function() {
            $('.modal_Load').hide();
        },
        error: function(data){
            console.log(data);
        }
    });
});

// $('.button_export_curr').click(function(){
//     return ExcellentExport.excel(this, 'datatable', '当前报表');
// });


